package tk.mybatis.springboot.mapper.Provider;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

public class OrderProvider {
	public String findOrderList(final Map<String, Object> params) {
		return new SQL(){{
			SELECT("tod.*, t.nickname as userName, t2.nickname as clerkName, c.name as goodsName");
			FROM("t_order tod");
			LEFT_OUTER_JOIN("t_user t on tod.userid = t.id");
			LEFT_OUTER_JOIN("t_user t2 on tod.countermanid = t2.id");
			LEFT_OUTER_JOIN("commodity c on tod.commodityid = c.id");
			if(params.get("createDateBegin") != null){
				WHERE("tod.createdate > #{createDateBegin}");
			}
			if(params.get("createDateEnd") != null){
				WHERE("tod.createdate < #{createDateEnd}");
			}
			if(params.get("orderNo") != null){
				WHERE("tod.orderno = #{orderNo}");
			}
			if(params.get("phone") != null){
				WHERE("tod.phone = #{phone}");
			}
			if(params.get("address") != null){
				WHERE("tod.address = #{address}");
			}
		}}.toString();
	}
	
	public String findOrderCount(final Map<String, Object> params) {
		return new SQL(){{
			SELECT("count(0)");
			FROM("t_order tod");
			LEFT_OUTER_JOIN("t_user t on tod.userid = t.id");
			LEFT_OUTER_JOIN("t_user t2 on tod.countermanid = t2.id");
			LEFT_OUTER_JOIN("commodity c on tod.commodityid = c.id");
			if(params.get("createDateBegin") != null){
				WHERE("tod.createdate > #{createDateBegin}");
			}
			if(params.get("createDateEnd") != null){
				WHERE("tod.createdate < #{createDateEnd}");
			}
			if(params.get("orderNo") != null){
				WHERE("tod.orderno = #{orderNo}");
			}
			if(params.get("phone") != null){
				WHERE("tod.phone = #{phone}");
			}
			if(params.get("address") != null){
				WHERE("tod.address = #{address}");
			}
		}}.toString();
	}
}
